import bean.Offer;
import com.corundumstudio.socketio.*;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import com.corundumstudio.socketio.listener.DisconnectListener;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wang.rongqiang on 2017/7/17.
 */
public class WebRtcService {
    final SocketIOServer socketIOServer;
    private List<SocketIOClient> mIOClients = new ArrayList<SocketIOClient>();

    public WebRtcService() throws URISyntaxException {
        Configuration config = new Configuration();
        config.setHostname("172.16.133.60");
        //设置监听端口
        config.setPort(8888);
        config.setSocketConfig(new SocketConfig());
        config.setUpgradeTimeout(10000000);
        config.setPingTimeout(10000000);
        config.setPingInterval(10000000);
        socketIOServer = new SocketIOServer(config);
        socketIOServer.addConnectListener(new ConnectListener() {
            public void onConnect(SocketIOClient client) {
                if (client != null) {
                    System.out.println("链接成功。。。id:" + client.getSessionId().toString());
                    mIOClients.add(client);
                    client.sendEvent("id", client.getSessionId().toString());

                    List<String> strings = new ArrayList<String>();
                    for (SocketIOClient ioClient : mIOClients) {
                        strings.add(ioClient.getSessionId().toString());
                    }
                    for (SocketIOClient ioClient : mIOClients) {
                        try {
                            List<String> templist = new ArrayList<String>();
                            for (String s : strings) {
                                if (!ioClient.getSessionId().toString().equals(s)) {
                                    templist.add(s);
                                }
                            }
                            ioClient.sendEvent("__datalist", templist);
                            System.out.println("发送客户端：id：" + ioClient.getSessionId() + "    data:" + templist.size());
                            System.out.println("列表:");
                            for (String s : templist) {
                                System.out.println(s + "\n");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                } else {
                    System.err.println("并没有人链接上。。。");
                }
                System.err.println(client.getSessionId().toString());
            }
        });

        socketIOServer.addDisconnectListener(new DisconnectListener() {
            public void onDisconnect(SocketIOClient client) {
                mIOClients.remove(client);
                System.out.println("退出连接。。。id:" + client.getSessionId().toString());
            }
        });
        //接收到发送请求
        socketIOServer.addEventListener("offer", Offer.class, new DataListener<Offer>() {
            public void onData(SocketIOClient client, Offer data,
                               AckRequest ackSender) throws Exception {
                System.out.println("收到呼叫");
                String toid = data.getToid();
                for (SocketIOClient ioClient : mIOClients) {
                    if (ioClient.getSessionId().toString().equals(toid)) {
                        System.out.println("我要发送呼叫给:" + toid + "  信息:" + data.toString() + "\n");

                        data.setFromid(client.getSessionId().toString());
                        ioClient.sendEvent("answer", data);
                    }
                }
            }
        });
        socketIOServer.addEventListener("answer", Offer.class, new DataListener<Offer>() {
            public void onData(SocketIOClient client, Offer data,
                               AckRequest ackSender) throws Exception {
                String toid = data.getToid();
                System.out.println("收到应答端回叫:" + client.getSessionId() + "  信息:" + data.toString() + "\n");
                for (SocketIOClient ioClient : mIOClients) {
                    if (ioClient.getSessionId().toString().equals(toid)) {
                        System.out.println("我要回传信息给:" + toid + "  信息:" + data.toString() + "\n");

                        data.setFromid(client.getSessionId().toString());
                        ioClient.sendEvent("offer_callback", data);
                    }
                }
            }
        });
        //获得列表
        socketIOServer.addEventListener("_getlist", String.class, new DataListener<String>() {
            public void onData(SocketIOClient client, String data,
                               AckRequest ackSender) throws Exception {
//                List<String> strings = new ArrayList<String>();
//                for (SocketIOClient ioClient : mIOClients) {
//                    if (ioClient != client) {
//                        strings.add(client.getSessionId().toString());
//                    }
//                }
//                client.sendEvent("__datalist", strings);
            }
        });
        //启动服务
        socketIOServer.start();
    }


    public SocketIOServer getSocketIOServer() {
        return socketIOServer;
    }
}
